package com.frejoys.app.controller;

import cn.hutool.core.util.StrUtil;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.common.login.VerifyCodeDto;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.EncryptUtil;
import com.frejoys.service.VerifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "验证码", description = "验证")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
@Authorization(Authority.Private)
public class VerifyCodeController {

    private final VerifyService verifyService;

    @Operation(summary = "发送验证码")
    @Authorization(Authority.Protected)
    @PostMapping("/verifyCode/send")
    public Result send(@RequestBody @Validated VerifyCodeDto dto) {
         //解密
         if (!StrUtil.isEmpty(dto.getAccountExtend())) {
             dto.setAccountExtend(EncryptUtil.appAes.decryptStr(dto.getAccountExtend()));
         }
         dto.setAccount(EncryptUtil.appAes.decryptStr(dto.getAccount()));

        verifyService.sendVerifyCode(dto.getAccountExtend(), dto.getAccount(), dto.getType());
        return Result.success();
    }

}
